import { DaHuaICCBaseService } from "./base";
import { dahua } from "./type";

export class PersonService extends DaHuaICCBaseService {
	/**
	 * 新增人员
	 * @param data 人员信息
	 * @returns 返回新增的人员ID
	 */
	async addPerson(data: dahua.icc.IAddPersonRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/add`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IAddPersonResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: { ...data, service: "evo-thirdParty" },
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 批量新增人员
	 * @description 1批次200人员，不推荐并发
	 * @param personList 人员信息列表
	 * @returns 返回新增的人员ID集合
	 */
	async batchAddPerson(personList: dahua.icc.IAddPersonRequest[]) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/batch-add`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IBatchAddPersonResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: {
				service: "evo-thirdParty",
				personList,
			},
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 更新人员信息
	 * @description 更新人员基本信息，包括生物特征数据
	 * @param data 人员信息
	 * @returns 返回更新的人员ID
	 */
	async updatePerson(data: dahua.icc.IUpdatePersonRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/update`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IUpdatePersonResponse>>(url, {
			method: "put",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: { ...data, service: "evo-thirdParty" },
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 获取人员详情
	 * @param personId 人员ID
	 * @returns 返回人员详细信息
	 */
	async getPersonInfo(personId: number) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/${personId}`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IPersonDetail>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 根据证件号获取人员详情
	 * @description 根据人员证件号信息获取人员详情
	 * @param paperNumber 证件号码
	 * @returns 返回人员详细信息
	 */
	async getPersonByPaperNumber(paperNumber: string) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/get-by-paper-number?paperNumber=${paperNumber}`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IPersonDetailByPaper>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: {},
		});
		this.handleResponseException(res);
		if (Object.keys(res.data.data).length === 0) return null;
		return res.data.data;
	}

	/**
	 * 删除人员
	 * @description 删除人员信息，支持批量删除，可选择是否同时删除关联的卡片和车辆信息
	 * @param params 删除参数
	 * @returns 返回删除结果
	 */
	async deletePerson(params: dahua.icc.IDeletePersonRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/person/delete`;
		const res = await this.curl<dahua.icc.IResponse<void>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: params,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 分页获取人员列表
	 * @description 根据查询条件分页获取人员信息列表
	 * @param params 查询参数
	 * @returns 返回分页的人员信息列表
	 */
	async getPersonList(params: dahua.icc.IPersonPageQuery) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/page`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IPageResponse<dahua.icc.IPersonDetailByPaper>>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: params,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 上传人员图片
	 * @description 上传人员图片，支持jpg、png、jpeg格式，大小5kb-4M
	 * @param imageBase64 图片base64字符串，需要包含前缀，如：data:image/png;base64,
	 * @returns 返回上传结果，包含文件路径和质量检测结果
	 */
	async uploadPersonImage(imageBase64: string) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/person/subsystem/third/upload/img`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IUploadImageResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: { imageBase64 },
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 生成人员全局ID
	 * @description 生成唯一的人员ID，用于新增人员时使用
	 * @returns 返回生成的人员ID
	 */
	async generatePersonId() {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/person/generate-id`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IGeneratePersonIdResponse>>(url, {
			method: "get",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 图片质量检测
	 * @description 检测图片质量，支持file、url、base64三种格式，可选择是否保存图片
	 * @param params 检测参数
	 * @returns 返回检测结果，包含文件路径和质量检测结果
	 */
	async checkImageQuality(params: dahua.icc.ICheckImageQualityRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/upload/img`;

		const res = await this.curl<dahua.icc.IResponse<dahua.icc.ICheckImageQualityResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
				"content-type": "multipart/form-data",
			},
			dataType: "json",
			data: {
				fileType: params.fileType,
				saveType: params.saveType,
				fileUrl: params.fileUrl,
				ossType: params.ossType,
			},

			// 使用FormData提交
			files: params.fileBase64 ? Buffer.from(params.fileBase64 || "", "utf-8") : undefined,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 冻结/解冻人员
	 * @description 冻结或解冻人员，冻结后权限会自动取消，解冻后会自动恢复
	 * @param params 冻结/解冻参数
	 * @returns 返回操作结果
	 */
	async freezePerson(params: dahua.icc.IFreezePersonRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/person/freeze`;
		const res = await this.curl<dahua.icc.IResponse<void>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: params,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 批量更新人员有效期
	 * @description 批量更新人员的权限有效期
	 * @param params 更新参数
	 * @returns 返回更新结果
	 */
	async batchUpdateValidTime(params: dahua.icc.IBatchUpdateValidTimeRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/batch-update-person-valid-time`;
		// 设置默认的service值
		const data = {
			...params,
			service: params.service || "evo-thirdParty",
		};

		const res = await this.curl<dahua.icc.IResponse<void>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 批量更新人员头像
	 * @description 批量更新人员的头像信息
	 * @param data 头像更新信息
	 * @returns 返回更新结果
	 */
	async batchUpdateBiosignatureFace(data: dahua.icc.IUpdateBiosignatureFaceRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.0.0/person/subsystem/update-biosignature-face`;
		const res = await this.curl<dahua.icc.IResponse<void>>(url, {
			method: "put",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 新增人员（包含车辆、卡片信息）
	 * @description 新增人员信息，同时可以添加关联的车辆和卡片信息
	 * @param data 人员信息（包含车辆、卡片信息）
	 * @returns 返回新增的人员ID
	 */
	async addPersonAll(data: dahua.icc.IAddPersonAllRequest) {
		const url = `${this.config.baseUrl}/evo-apigw/evo-brm/1.2.0/person/subsystem/save-person-all`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.IAddPersonResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data: { ...data, service: "evo-thirdParty" },
		});
		this.handleResponseException(res);
		return res.data.data;
	}
}
